home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / SPX20.ZIP / SPX_DOC.ZIP / SPX_TIM.DOC < prev    next >
Text File  |  1993-09-15  |  6KB  |  170 lines

  1. { SPX Library Version 2.0  Copyright 1993 Scott D. Ramsay }
  2.  
  3.   The SPX_TIM units is clock timer unit.  It is the same as the counters
  4.  in the unit SPX_SND but maintains more counters.  12 clock timers to
  5.  use for game precision.
  6.  
  7.      s_clk : array[0..5] of word;  { slow counters }
  8.      f_clk : array[0..5] of word;  { fast counters }
  9.  
  10.   The unit takes control of the computer's clock interrupt and allows
  11. the changing of its interrupt frequency.  From its regular 18.2 times per
  12. second.
  13.  
  14.   The slow counters always count down at 18.2 times a second. Regardless of
  15. the clock interrupt's rate. So, for example, the loop below will wait
  16. for about 5 seconds:
  17.  
  18.    s_clk[0] := 5*18;
  19.    repeat
  20.    until s_clk[0]=0;
  21.  
  22.   Notice that there is nothing in the repeat loop.  The counters are
  23. automatically decremented.  The counters also do not roll over. They will
  24. decrement until they reach zero then stop.
  25.  
  26.   The fast counters count down according to the clock rate.  The clock rate
  27. can be changed by the following procedure:
  28.  
  29.     procedure setrate(cycles:word);
  30.  
  31.   The variable cycles indicates the number of cycles(interrupts) to generate
  32. per second.  So  setrate(18);  will restore the clock to its original rate.
  33. Setrate(1000);  will generate an interrupt 1000 times per second.  Warning some
  34. slow computers (8086/286) can not handle high clock rates.  Also conflicts
  35. may occur if running under MS Windows or OS/2.
  36.  
  37.    Here's another example:
  38.  
  39.       setrate(2048);  { set the clock rate }
  40.       f_clk[0] := 4096;
  41.       repeat
  42.       until f_clk[0]=0;
  43.  
  44.    The above example will loop for 2 seconds.
  45.  
  46.    A nice thing about the counters is that you can use them to
  47. regulate the speed of your program/game.  Lets make a VERY rough
  48. example.  Lets suppose you wrote a game that you want each 'frame'
  49. or pass to take 1 second.  Your game loop would look something
  50. like this:
  51.  
  52.      repeat
  53.        s_clk[0] := 18;
  54.        { grab user inputs }
  55.        { do game calcuations }
  56.        { setup objects for display }
  57.        { update visual screen }
  58.        while s_clk[0]<>0 do;
  59.      until game_over;
  60.  
  61.    If one pass of the loop is faster than one second, then the game
  62. will wait in the while loop until a second is up. Now depending on the
  63. speed of the computer, the number of calcuations, speed of displaying the
  64. sprites, the wait time may vary.  But the above loop will always wait
  65. at MOST one second.  Now if one pass takes longer than a second then the
  66. counter is already reached zero so there is no delay.  So the loop is
  67. running at maximum speed.
  68.  
  69.    Now for event loops in games we WANT it to run alot faster than 1 sec.
  70. So we would use a fast clock counter.
  71.  
  72.      setrate(3000);
  73.      repeat
  74.        f_clk[0] := 80;
  75.        { grab user inputs }
  76.        { do game calcuations }
  77.        { setup objects for display }
  78.        { update visual screen }
  79.        while f_clk[0]<>0 do;
  80.      until game_over;
  81.  
  82.    Now were all set.  We can even get fancy.  We can even automatically
  83. adjust the rate according to the cabilities of the machine;
  84.  
  85.      var
  86.        crate    : word;
  87.  
  88.      setrate(3000);
  89.      crate := 80;
  90.      repeat
  91.        f_clk[0] := crate;
  92.        { grab user inputs }
  93.        { do game calcuations }
  94.        { setup objects for display }
  95.        { update visual screen }
  96.        if (crate>0) and (f_clk[0]<10)
  97.          then dec(crate);
  98.        while f_clk[0]<>0 do;
  99.      until game_over;
  100.  
  101.    Now if the computer can't keep up with the rate (The clock comes
  102. close to timing out).  The rate is speeded up.
  103.  
  104. NOTES:
  105.       This unit will not work in protected mode because of changing the clock
  106. rate.  As a result the the f_clk counters will act as s_clk counters.  Such
  107. in a Windows DOS box. etc.
  108. ───────────────────────────────────────────────────────────────────────────
  109. GLOBAL VARIABLES:
  110.  
  111.       f_clk:     Fast clock counters
  112.       s_clk:     Slow clock counters
  113.       rate:      Current fast clock rate
  114.       cntime:    Reserved counter, Do not modify
  115.   --------------------------------------------------------
  116.       f_userclk,
  117.       s_userclk   : userproc;
  118.  
  119.     F_USERCLK and S_USERCLK allows you to add your own custom routines
  120.   at each clock interrupt.  Since these procedures are called at every
  121.   interrupt you have to avoid a few things:
  122.  
  123.       1.  You routine should be fast as possible. Since it will be called
  124.          many times.  if it does to many calculations, it will slow the
  125.          program down.
  126.  
  127.       2. Do not call any interupts.  You are already in one.
  128.  
  129.       3. Avoid disk access.  It may or may not work.  Don't try it!
  130.  
  131.       4. Your routine must be declared as a far procedure with no
  132.          parameters.
  133.  
  134.   Example:
  135.  
  136.     Uses spx_tim;
  137.  
  138.     var
  139.       l,s : longint;
  140.  
  141.     procedure MyUserClk;  far;
  142.     begin
  143.       inc(l);
  144.     end;
  145.  
  146.  
  147.     begin
  148.       l := 0;
  149.       s_userclk := MyUserClk;
  150.       readln; s := l;
  151.       writeln('My function was called ',s,' times while waiting');
  152.     end.
  153.  
  154. ───────────────────────────────────────────────────────────────────────────
  155. procedure setrate(cycles:word);
  156.  
  157.   Changes the clock rate of the computer.
  158.  
  159.   CYCLES:  Number of interrupts to generate per second
  160.  
  161. ───────────────────────────────────────────────────────────────────────────
  162. procedure wait(seconds,which:integer);
  163.  
  164.   Wait for a specified number of seconds.
  165.  
  166.   SECONDS:  Number of seconds to wait;
  167.   WHICH:    Index number of slow clock counter to use.  (0..3)
  168.  
  169. ───────────────────────────────────────────────────────────────────────────
  170.